<?php

App::uses('AppController', 'Controller');

class BuildingController extends AppController {

    /**
     * set layout
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel('Building');
        $this->set('title_for_layout', "building");
        $this->menu_active = 'building';
    }

    /**
     * 
     */
    public function index() {
        $condition = array();
        $name = $this->request->query('name');
        if (isset($_GET['submit'])) {
            if (!empty($_GET['Other'])) {
                $condition['OR'] = array(
                    'Building.BuildId like' => '%' . $_GET['Other'] . '%',
                    'Building.BuildName like' => '%' . $_GET['Other'] . '%',
                    'Building.BuildAddress like' => '%' . $_GET['Other'] . '%',
                    'Building.BuildPhone like' => '%' . $_GET['Other'] . '%',
                );
            } else {
                $condition['AND'] = array(
                    'Building.BuildId like' => '%' . $_GET['Id'] . '%',
                    'Customer.CustName like' => '%' . $_GET['CustName'] . '%',
                    'Building.BuildName like' => '%' . $_GET['BuildName'] . '%'
                );
            }
        }

        $buildings = $this->Building->getAllBuilding($condition);
        $this->set('buildings', $buildings);
    }

    /**
     * 
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Building->create();
            $this->Building->data['Building']['CrtDt'] = date('Y-m-d H:i:s');
            $this->Building->data['Building']['UpdDt'] = date('Y-m-d H:i:s');
            if ($this->Building->save($this->request->data)) {
                $this->Session->setFlash(__('Your Building has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to add your Building.'));
        }
    }

    /**
     * 
     */
    public function edit($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid Building'));
        }

        $building = $this->Building->find('first', array('conditions' => array('Building.Id' => $id), 'order' => 'Building.CrtDt DESC'));
        if (!$building) {
            throw new NotFoundException(__('Invalid Building'));
        }

        if ($this->request->is(array('post', 'put'))) {
            $this->Building->id = $id;
            $this->Building->data['Building']['UpdDt'] = date('Y-m-d H:i:s');
            if ($this->Building->save($this->request->data)) {
                $this->Session->setFlash(__('Your Building has been updated.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your Building.'));
        }

        if (!$this->request->data) {
            $this->request->data = $building;
        }
        $this->render('add');
    }

    /**
     * 
     */
    public function delete($id = null) {
        if ($id == null) {
            throw new NotFoundException(__('Invalid Building'));
        }

        if ($this->Building->delete($id)) {
            $this->Session->setFlash(__('The Building with id: %s has been deleted.', h($id)));
            return $this->redirect(array('action' => 'index'));
        }
    }

}
